package com.feng.study.demo.lambda;

import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.function.Predicate;
import java.util.stream.Collectors;

/**
 * 函数式接口：都有@FunctionalInterface注解修饰
 * 1.Consumer<T>  消费型接口
 * 2.Supplier<T> 供给型接口
 * 3.Function<T,R> 函数型接口
 * 4.Predicate<T> 断言型接口
 * @author E
 */
@Slf4j
public class TestPredicate {
    public static void main(String[] args) {
        testConsumer();
//        testFunction();
//        testPredicate();
    }

    /**
     * Consumer:消费型接口
     */
    private static void testConsumer(){
        List<Integer> list = Lists.newArrayList(1,2,3,4,1,1,1);
        Consumer<Integer> consumer= new Consumer<Integer>() {
            @Override
            public void accept(Integer integer) {
                int a=integer+2;
                System.out.println(a);
            }
        };
        List<Integer> collect = list.stream().peek(p -> log.info("链表值:{}",p+10)).collect(Collectors.toList());
    }
    private static void testFunction(){
        List<Integer> list = Lists.newArrayList(1,2,3,4,1,1,1);
        Function<Integer,String> function = new Function<Integer, String>() {
            @Override
            public String apply(Integer integer) {
                return integer.toString();
            }
        };
        System.out.println(function.apply(10));
        list.stream().map(p->p.toString());
    }
    /**
     * Predicate：断言型接口，判断型接口
     */
    private static void testPredicate(){
        List<Integer> list = Lists.newArrayList(1,2,3,4,1,1,1);
        Predicate<Integer> predicate=new Predicate<Integer>() {
            @Override
            public boolean test(Integer integer) {
                return integer==1;
            }
        };
        boolean test = predicate.test(10);
        System.out.println(test);
        Predicate<Integer> predicate1=f->f==1;
        list.stream().filter(predicate);
        list.stream().filter(predicate1);
        list.stream().filter(f->f==1);
    }
}
